home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / rkeyboar.cpt / Reactive Keyboard ƒ / RK_BUTTON.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-20  |  10.9 KB  |  385 lines

  1. #include <MacIncludes.h>
  2. #include "rk_button.h"             
  3. #include "file+rk.h"
  4. #include "Rk.h"
  5. #include "Structs.h"
  6.  
  7. extern PrefHandle gStartPrefs;
  8. extern char *p2cstr(Str255);
  9. extern char *PathNameFromDirID(long DirID, short vRefNum, char *s);
  10. extern char *pStrcat(Str255,Str255);
  11. extern Boolean gLowMem,gReport;
  12. Ptr       gExtraMems;
  13.  
  14.  
  15.                      /** shared with rk_file/file+rk **/
  16. char   first[MAX_SET],             /* the first letter of each pred */
  17.        context[MAX_CMD_LINE_LENGTH] = "\07",   /* last chars entered */
  18.        old_context[MAX_CMD_LINE_LENGTH] = "\07";
  19. Buffer Buf, CBuf;             /* ptrs into the model k levels  */
  20. char   *prime_file;             /* file to prime from and log to */
  21. int    next_free = 0;             /* index of next free node avail */
  22. long   psize;                 /* # chars to prime from prime_file*/
  23.                      /* zero freqs, maybe overwritten */
  24. static char zero_freq[128] = {    
  25.   '\n',   ' ',   'e',   's',   't',   'r',   'a',   'l',
  26.    'n',   '.',   'c',   'i',   'm',   'o',   'd',   'h',
  27.    'p',   'u',   'f',   'b',   'w',   'g',   '-',   'y',
  28.    '/',   'v',   'k',   '*',   'x',   '5',   '1',   '2',
  29.    '4',   '>',   'q',   '`',   '3',   'z',   '0',   'j',
  30.    'M',   'I',   '6',  '\'',   'E',   '~',   'A',   ',',
  31.    'S',   'D',   'R',   '?',   'C',   '|',   'B',   'T',
  32.    'P',   'U',   '!',   '_',   '<',   'N',   '8',   '@',
  33.    '&',   '7',  '\\',   '"',   'J',   ')',   '(',   '[',
  34.    ']',   'F',   'L',   ':',   'O',   'K',   '9',   'H',
  35.    '+',   '$',   'W',   'Y',   '=',   'G',   ';',   '^',
  36.    '{',   'X',   'Q',   '#',   '}',   'V',   '%',   'Z',
  37. /*   '`',  '\b',  '\t', '\26', '\00', '\01', '\02', '\03',
  38.  '\04', '\05', '\06', '\13',  '\f',  '\r', '\16', '\17',
  39.  '\20', '\21', '\22', '\23', '\24', '\25', '\27', '\30',
  40.  '\31', '\32', '\33', '\34', '\35', '\36', '\37','\177',*/};
  41.  
  42. char *zero_freq_file;
  43.  
  44. static char    pred_set[MAX_SET];    /* flags chars already in first[]*/
  45.  
  46. static NodePtr free_nodes;         /* now use malloc to get at init */
  47. static NodePtr root;             /* the root of the k model trie  */
  48.  
  49. NodePtr create_node() {             /* return ptr to a new node or abort */
  50.     char *malloc(); NodePtr nptr;
  51.     if(!gLowMem){
  52.         nptr = (NodePtr) malloc((unsigned) sizeof(Node));
  53.         nptr->next=nptr->up=nil;
  54.     }else nptr=nil;
  55.     if (nptr == nil){        /* SHOULD FORGET AND CONTINUE ON */
  56.         gLowMem = true;
  57.         gReport = false;
  58.         if(gExtraMems) DisposPtr(gExtraMems);
  59.         return(nil);
  60.     }
  61.     next_free++;              /* just for show_free_nodes */
  62.     return(nptr);              /* if more are needed   */
  63. }
  64.  
  65. NodePtr move_up(nptr, c) NodePtr nptr; char c; {
  66.  
  67.     NodePtr xptr, fxptr, last_ptr, last_fptr; int state;
  68.  
  69.     /* c &= 0x7F; */
  70.  
  71.     if (nptr == nil) xptr = nil;
  72.     else {
  73.          if (nptr->up == nil) state = START;
  74.          else {
  75.             fxptr = xptr = nptr->up;  last_fptr = last_ptr = nil;
  76.             state = SCANNING;
  77.             do {
  78.                if (xptr == nil) state = END;
  79.                else {
  80.                   if (fxptr->count > xptr->count) {
  81.              last_fptr = last_ptr;  fxptr = xptr; }
  82.                   if (xptr->value == c) {
  83.                      state = FOUND;
  84.                      if (fxptr != xptr) {
  85.                         if (last_fptr == nil) nptr->up = xptr;
  86.                         else last_fptr->next = xptr;
  87.                         last_ptr->next = xptr->next;  xptr->next = fxptr;
  88.                   }  } else { last_ptr = xptr;  xptr = xptr->next; }
  89.             }  } while (state == SCANNING);
  90.          }
  91.          switch (state) {
  92.          case FOUND:
  93.             if (++(xptr->count) == (*gStartPrefs)->pMaxCount) { /* Forgets on halving 1 */
  94.                 /*last_fptr =*/ fxptr = nptr->up;
  95.                 while (fxptr != nil) {         /* JJD 9-86 */
  96.                 fxptr->count++;
  97.                 fxptr->count >>= 1;
  98.                     fxptr = fxptr->next;
  99.                 
  100.                 }
  101.             }
  102.             break;
  103.          case START: case END:
  104.                xptr = create_node();
  105.                if (xptr){
  106.                        xptr->value = c;  xptr->count = 1;
  107.                        xptr->up = xptr->next = nil;
  108.                        if (state == START) nptr->up = xptr;
  109.                        else last_ptr->next = xptr;
  110.                }
  111.                break;
  112.         }
  113.     }
  114.     return (xptr);
  115. }
  116.  
  117. find_first(buf) Buffer buf; { /* find 1st char of all pred in context */
  118.  
  119.     int i, order = 0; NodePtr xptr; char *p;
  120.  
  121.         for (p= &pred_set[0]; p< &pred_set[MAX_SET]; p++) *p = '\0';
  122.         for (i=(*gStartPrefs)->pOrder-1; i>=0; i--) {      
  123.         if (buf[i] != nil)
  124.         if (buf[i]->up != nil) {
  125.         xptr = buf[i]->up;
  126.         while (xptr != nil) {
  127.             if (!pred_set[(int)xptr->value]) {
  128.             pred_set[(int)xptr->value]++;
  129.             first[order++] = xptr->value;
  130.             }  
  131.             xptr = xptr->next;
  132.     }   }    }
  133.         for (p= &zero_freq[0]; p< &zero_freq[MAX_SET]; p++)
  134.         if (!pred_set[(int)*p]) first[order++] = *p;
  135. }
  136.  
  137. char first_pred(buf) Buffer buf; {
  138.     int i = (*gStartPrefs)->pOrder-1;
  139.     for (;;) {
  140.     if (buf[i] != nil)
  141.         if (buf[i]->up != nil) return (buf[i]->up->value);
  142.     if (i == 0) return((char)13);
  143.     i--;
  144.     }
  145. }
  146.  
  147. NodePtr scan_up(nptr,c) NodePtr nptr; char c; {
  148.  
  149.     NodePtr xptr;
  150.     if (nptr == nil) return(nil);
  151.     else {
  152.         xptr = nptr->up;
  153.         for (;;) {
  154.             if (xptr == nil) return (nil);
  155.             else if (xptr->value == c) return(xptr);
  156.             else xptr = xptr->next;
  157.     }    }
  158. }
  159.  
  160. #define MemSize 30720
  161.  
  162. struct MemBlck{
  163.     char Memory[MemSize+4];
  164.     struct MemBlck *Next;
  165. } *MemBlock;
  166.  
  167. int NumMBlocks=0;
  168.  
  169. char* MemNext=MemSize;
  170. char *malloc(size)
  171. {
  172.     size=(size+4)&~4;
  173.     if(MemNext>size){
  174.         MemNext-=size;
  175.         return(char *)&(MemBlock->Memory[MemNext]);
  176.     }else{
  177.         struct MemBlck *new=NewPtrClear(sizeof(struct MemBlck));
  178.         if(new==nil)
  179.             return((char *) 0);
  180.         NumMBlocks++;
  181.         new->Next=MemBlock;
  182.         MemBlock=new;
  183.         MemNext=(char *)MemSize-size;
  184.         return (char *)&(MemBlock->Memory[MemNext]);
  185.     }
  186. }
  187.         
  188. SetUpMemory()
  189. {
  190.     MemBlock=(struct MemBlck *)NewPtrClear(sizeof(struct MemBlck));
  191.     MemBlock->Next=0;
  192.     MemNext=(char *)MemSize;
  193.     NumMBlocks=0;
  194. }
  195.  
  196. ClearMemory()
  197. {
  198.     struct MemBlck *next;
  199.     while(MemBlock){
  200.         next=MemBlock->Next;
  201.         DisposPtr((Ptr)MemBlock);
  202.         MemBlock=next;
  203.     }
  204.     SetUpMemory();
  205. }    
  206.  
  207. ClearModel()
  208. {
  209.     int i,c;
  210.     FILE *from;
  211.     char s[255];
  212.  
  213.  
  214.     next_free=0;
  215.     ClearMemory();    
  216.     
  217.     CBuf[0] = Buf[0]        = root = create_node();
  218.     root->up    = root->next = nil;
  219.     root->count = 1;
  220.     for (i=1; i<=(*gStartPrefs)->pOrder; i++) Buf[i] = nil;
  221.     if ((*gStartPrefs)->pHasZero){
  222.     PathNameFromDirID((*gStartPrefs)->pZeroDirID,
  223.                       (*gStartPrefs)->pZeroRefNum,s);
  224.     pStrcat(s,(Str255)(*gStartPrefs)->pZeroName);
  225.         if ((from = fopen (p2cstr(s), "r")) != NULL) {
  226.             i = 0;
  227.             while (((int)(c = getc(from))) != EOF) {
  228.                     zero_freq[i++] = c;
  229.                     if (((int)(c = getc(from))) == EOF) break; /* del NL */
  230.                 if (i>127) break;             /* test if okay */
  231.             }
  232.         } /* else use built in zero_freq[] */
  233.     }
  234. }    
  235.  
  236. PrimeFromFile(char *name,Boolean clear)
  237. {
  238.     int         c,i,psize;
  239.     long         size,graph;
  240.     FILE         *from;
  241.     DialogPtr     Therm;
  242.     Rect        ThermRect,GrayRect;
  243.     short         newRight;
  244.     
  245.     if(clear){
  246.         ClearModel();
  247.     }
  248.     psize = (long) ((*gStartPrefs)->pStartup);  /* increase?? */
  249.     if ((from = fopen (name, "r")) != NULL) {
  250.         fseek (from, 0L, 2);        /* find out how long it is */
  251.         size = ftell (from);
  252.         if (size > psize) {            /* prime max chars at end  */
  253.             size=psize;
  254.             fseek (from, -psize, 2);           /* start after ^G mark  */
  255.         } else rewind (from);
  256.         graph=0;
  257.         if (size==0) size=1;
  258.         Therm=GetNewDialog(rPrimeTherm,(void *) 0,(void *) -1);
  259.         DrawDialog(Therm);
  260.         SetRect(&ThermRect,55,68,227,89);
  261.         SetRect(&GrayRect,56,69,57,88);
  262.         SetPort(Therm);
  263.         FrameRect(&ThermRect);
  264.         while (((int)(c = getc(from))) != EOF) {
  265.             RotateCursor(graph++);
  266.             newRight = ThermRect.left + graph * 
  267.             (ThermRect.right - ThermRect.left - 1) / size;
  268.             if (newRight > ThermRect.right) newRight = ThermRect.right;
  269.             if (newRight > GrayRect.right) {
  270.                 GrayRect.left = GrayRect.right-1;
  271.                 GrayRect.right = newRight;
  272.                 EraseRect(&GrayRect);
  273.                 FillRect(&GrayRect, qd.gray);
  274.             };
  275.             for (i=(*gStartPrefs)->pOrder; i>0; i--) 
  276.                 Buf[i] = move_up(Buf[i-1],c);
  277.         }
  278.         fclose (from);
  279.         DisposDialog(Therm);
  280.         Show_Cursor(ARROW_CURSOR);
  281.     }
  282. }
  283.  
  284. init_reactive() {
  285.     double atof();
  286.     register int i; FILE *from, *popen();
  287.     char c, *rindex();
  288.     char cbuf[32+1], *cstart, *cend, *end; int full; long size,graph;
  289.     DialogPtr Therm;
  290.     Rect    ThermRect,GrayRect;
  291.     short newRight;
  292.     char     s[255];
  293. /*     char    debug[255];*/
  294.     
  295.     
  296.                 /* get a bunch of nodes for starters        */
  297. /*    free_nodes = (NodePtr) malloc((unsigned) (max_nodes * sizeof(Node)));
  298.     if (free_nodes == nil) {
  299.             sprintf (tbuf, "cannot allocate %d nodes.\n", max_nodes);
  300.             abortit (tbuf, -1);
  301.     } */
  302.                 /* set up root and pointers into model      */
  303.     CBuf[0] = Buf[0]        = root = create_node();
  304.     root->up    = root->next = nil;
  305.     root->count = 1;
  306.     for (i=1; i<=(*gStartPrefs)->pOrder; i++) Buf[i] = nil;
  307.     
  308.     if ((*gStartPrefs)->pHasZero){
  309.         if(PathNameFromDirID((*gStartPrefs)->pZeroDirID,
  310.                           (*gStartPrefs)->pZeroRefNum,s) != -1){
  311.             pStrcat(s,(Str255)(*gStartPrefs)->pZeroName);
  312.             if ((from = fopen (p2cstr(s), "r")) != NULL) {
  313.                 i = 0;
  314.                 while (((int)(c = getc(from))) != EOF) {
  315.                     zero_freq[i++] = c;
  316.                     if (((int)(c = getc(from))) == EOF) break; /* del NL */
  317.                     if (i>127) break;             /* test if okay */
  318.                 }
  319.             } else /* else give warning and use built in zero_freq[] */
  320.                 AlertUser(eCantZero); 
  321.         } else /* else give warning and use built in zero_freq[] */
  322.             AlertUser(eCantZero); 
  323.     }
  324.     
  325.  
  326.     psize = (long) ((*gStartPrefs)->pStartup);
  327.     if((*gStartPrefs)->pHasPrime){
  328.         if(PathNameFromDirID((*gStartPrefs)->pPrimeDirID,
  329.                       (*gStartPrefs)->pPrimeRefNum,s) != -1){
  330.             pStrcat(s,(Str255)(*gStartPrefs)->pPrimeName);
  331.             if ((from = fopen(p2cstr(s),"r")) != NULL) {
  332.     
  333.                 fseek (from, 0L, 2);        /* find out how long it is */
  334.                 size = ftell (from);
  335.                 if (size > psize) {            /* prime max chars at end  */
  336.                     size=psize;
  337.                     fseek (from, -psize, 2);           /* start after ^G mark  */
  338.                 } else rewind (from);
  339.                 graph=0;
  340.                 if (size==0) size=1;
  341.                 Therm=GetNewDialog(rPrimeTherm,(void *) 0,(void *) -1);
  342.                 DrawDialog(Therm);
  343.                 /* shouldn't really hardcode this */
  344.                 SetRect(&ThermRect,55,68,227,89);
  345.                 SetRect(&GrayRect,56,69,57,88);
  346.                 SetPort(Therm);
  347.                 FrameRect(&ThermRect);
  348.                 cstart = cend = cbuf; end = &cbuf[(*gStartPrefs)->pOrder]; full = 0;
  349.                 while (((int)(c = getc(from))) != EOF) {
  350.                     RotateCursor(graph++);
  351.                     newRight = ThermRect.left + graph * 
  352.                     (ThermRect.right - ThermRect.left - 1) / size;
  353.                     if (newRight > ThermRect.right) newRight = ThermRect.right;
  354.                     if (newRight > GrayRect.right) {
  355.                         GrayRect.left = GrayRect.right-1;
  356.                         GrayRect.right = newRight;
  357.                         EraseRect(&GrayRect);
  358.                         FillRect(&GrayRect, qd.gray);
  359.                     };
  360.         
  361.                     *cstart = c;
  362.                     if (full)
  363.                         { ++cstart; if (cstart > end) cstart = cbuf; }
  364.                     if (++cend > end)
  365.                         { cend = cbuf; full = 1; }
  366.                     *cend = '\0';
  367.                     for (i=(*gStartPrefs)->pOrder; i>0; i--) 
  368.                         Buf[i] = move_up(Buf[i-1],c);
  369.                 }
  370.                 i = 0;        /* align the context */
  371.                 while (cstart != cend) {
  372.                     context[i] = old_context[i] = *cstart++; i++;
  373.                     if (cstart > end) cstart = cbuf;
  374.                 }
  375.                 fclose (from);
  376.                 DisposDialog(Therm);
  377.                 Show_Cursor(ARROW_CURSOR);
  378.             }else 
  379.                 AlertUser(eCantPrime);
  380.         }else 
  381.             AlertUser(eCantPrime);
  382.     }
  383. }
  384.  
  385.